#include<bits/stdc++.h>
using namespace std;
const int N=1e7+7;
int T,n,lst,A,B,C,M,cnt[N];
int gt(int x){
    return(1ll*x*x%M*A%M+1ll*x*B%M+C)%M+1;
}
bool ty[N];
int tot;
const double eps=1e-8;
double u[N];
bool check(double nw){
    for(int i=1;i<=tot;i++)
        if(ty[i])u[i]=1-nw;
        else u[i]=1;
    for(int i=1;i<tot;i++){
        if(u[i]<2*nw){
            u[i+1]-=2*nw-u[i];
            if(u[i+1]<0)
                return 0;
        }
    }return 1;
}
const int mod=998244353;
int x[N],y[N],lx,ly;
void upd(int u,int v){
    if(1ll*u*ly<1ll*lx*v){
        lx=u;
        ly=v;
    }
    return;
}
int pw(int a,int p){
    int s=1;
    while(p){
        if(p&1)s=1ll*s*a%mod;
        a=1ll*a*a%mod;p>>=1;
    }return s;
}
int gt(double nw){
    lx=ly=1;
    for(int i=1;i<=tot;i++)
        if(ty[i]){
            u[i]=1-nw;
            x[i]=-1;
            y[i]=1;
        }
        else{
            u[i]=1;
            x[i]=0;
            y[i]=1;
        }
    for(int i=1;i<tot;i++){
        if(u[i]<2*nw){
            u[i+1]-=2*nw-u[i];
            x[i+1]-=2-x[i];
            y[i+1]+=y[i];
        }
    }
    for(int i=1;i<=tot;i++)
        if(x[i]<0&&y[i]>0){
            upd(y[i],-x[i]);
        }
    return 1ll*lx*pw(ly,mod-2)%mod;
}
signed main(){
    freopen("guess.in","r",stdin);
    freopen("guess.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);cout<<fixed<<setprecision(10);
    cin>>T;
    while(T--){
        tot=0;cin>>n>>lst>>A>>B>>C>>M;
        for(int i=1;i<=n;i++){
            lst=gt(lst);
            cnt[lst]++;
        }
        for(int i=1;i<=n;i++)
            if(cnt[i]==1){
                ty[++tot]=0;
            }else if(cnt[i]>1)
                ty[++tot]=1;
        double l=0,r=1;
        while(r-l>eps){
            double mid=(l+r)/2;
            if(check(mid)){
                l=mid;
            }else r=mid;
        }
        cout<<l<<" "<<gt(l)<<"\n";
        for(int i=1;i<=n;i++)
            cnt[i]=0;
    }
    return 0;
}
